package com.neusoft.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.constants.EditableProperties;
import com.neusoft.mapper.CreditMapper;
import com.neusoft.po.Credit;
import com.neusoft.service.CreditService;
import com.neusoft.util.CommonUtil;

@Service
public class CreditServiceImpl implements CreditService {
	@Autowired
	private CreditMapper creditMapper;

	@Transactional
	@Override
	public Integer getTotalCreditByUserId(String userId) {

		List<Credit> list = creditMapper.listAvailableCreditByUserId(userId);
		Credit credit = new Credit();
		// 同时把过期和用完的积分记录更新
		Integer totalCredit = credit.getTotalNum(list);
		for (Credit c : list)
			creditMapper.updateCredit(c);
		return totalCredit;
	}

	@Transactional
	@Override
	public List<Credit> listAvailableCreditByUserId(String userId) {
		List<Credit> list = creditMapper.listAvailableCreditByUserId(userId);
		List<Credit> returnlist = new ArrayList<>();
		// 删除过期积分
		for (Credit credit : list) {
			if (credit.isAvailable()) {
				returnlist.add(credit);
			} else {
				creditMapper.updateCredit(credit);
			}
		}
		return returnlist;
	}

	@Override
	public List<Credit> listAllCreditByUserId(String userId) {
		return creditMapper.listAllCreditByUserId(userId);
	}

	@Override
	public List<Credit> listCreditByUserIdAndChannelType(String userId, int channelType) {
		return creditMapper.listCreditByUserIdAndChannelType(userId, channelType);
	}

	@Override
	public int insertCredit(Credit credit, BigDecimal count) {
		int num = 0;
		// 根据来源进行倍率换算
		if (credit.getChannelType() == 1) {
			num = count.multiply(EditableProperties.RATIO_GET_CHANNEL_1).intValue();
		}
		if (count.equals(BigDecimal.ZERO)) {
			num = credit.getNum();
		}
		Long time = System.currentTimeMillis();
		Long expiredTime = time + EditableProperties.VALID_TIME;
		Credit c = new Credit(credit.getUserId(), credit.getChannelType(), num, expiredTime);
		return creditMapper.insertCredit(c);
	}

	@Transactional
	@Override
	public int useCredit(Credit credit, BigDecimal count) {
		// TODO Auto-generated method stub
		List<Credit> list = creditMapper.listAvailableCreditByUserId(credit.getUserId());
		Integer totalCredit = credit.getTotalNum(list);
		int num = count.multiply(EditableProperties.RATIO_BUY).intValue();
		if (totalCredit > num) {
			Credit c = credit.useCredit(list, num);
			if (c != null) {
				creditMapper.insertCredit(c);
			}
		} else if (totalCredit > num) {
			credit.useOut(list);
		} else {
			return 0;// 积分不足
		}

		for (Credit c : list)
			creditMapper.updateCredit(c);
		// 负值代表消费积分
		num = -1 * num;
		// 积分消费记录
		Credit c = new Credit(credit.getUserId(), credit.getChannelType(), num,
				System.currentTimeMillis() + EditableProperties.VALID_TIME);
		return creditMapper.insertCredit(c);
	}
}
